package com.holly.unit.system.modular.user.controller;

import com.holly.unit.auth.api.AuthServiceApi;
import com.holly.unit.auth.api.SessionManagerApi;
import com.holly.unit.auth.api.UserDataMapApi;
import com.holly.unit.auth.api.context.LoginContext;
import com.holly.unit.auth.api.pojo.auth.LoginRequest;
import com.holly.unit.auth.api.pojo.auth.LoginResponse;
import com.holly.unit.auth.api.pojo.auth.LoginWithTokenRequest;
import com.holly.unit.auth.api.pojo.login.LoginUser;
import com.holly.unit.core.pojo.response.ResponseData;
import com.holly.unit.core.pojo.response.SuccessResponseData;
import com.holly.unit.scanner.api.annotation.ApiResource;
import com.holly.unit.scanner.api.annotation.GetResource;
import com.holly.unit.scanner.api.annotation.PostResource;
import com.holly.unit.system.api.pojo.login.CurrentUserInfoResponse;
import com.holly.unit.system.api.pojo.login.ValidateTokenRequest;
import com.holly.unit.system.modular.user.factory.UserLoginInfoFactory;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * 登录登出控制器
 *
 * @author holly
 * @date 2021/3/17 17:23
 */
@RestController
@Slf4j
@ApiResource(name = "登录登出管理")
@Api(tags = "登录登出管理")
public class LoginController {

    @Resource
    private AuthServiceApi authServiceApi;

    @Resource
    private SessionManagerApi sessionManagerApi;

    @Resource
    private UserDataMapApi userDataMapApi;

    /**
     * 用户登录
     *
     * @author holly
     * @date 2021/3/17 17:23
     */
    @ApiOperation(value = "登录" ,notes = "登录")
    @PostResource(name = "登录", path = "/login", requiredLogin = false, requiredPermission = false, responseClass = String.class)
    public ResponseData login(@RequestBody @Validated LoginRequest loginRequest) {
        loginRequest.setCreateCookie(true);
        LoginResponse loginResponse = authServiceApi.login(loginRequest);
        return new SuccessResponseData(loginResponse.getToken());
    }

    /**
     * 用户登录(提供给分离版用的接口，不会写cookie)SysResourceServiceImpl
     *
     * @author holly
     * @date 2021/3/17 17:23
     */
    @ApiImplicitParam(name = "loginRequest",value = "登录的请求参数实体",paramType = "body", required = true, dataType = "LoginRequest")
    @ApiOperation(value = "登录（分离版）" ,notes = "登录（分离版）")
    @PostResource(name = "登录（分离版）", path = "/loginApi", requiredLogin = false, requiredPermission = false, responseClass = LoginResponse.class)
    public ResponseData loginApi(@RequestBody @Validated LoginRequest loginRequest) {
        loginRequest.setCreateCookie(false);
        LoginResponse loginResponse = authServiceApi.login(loginRequest);
        return new SuccessResponseData(loginResponse);
    }


    @ApiOperation(value = "获取用户信息" ,notes = "获取用户信息")
    @GetResource(name = "获取用户信息", path = "/getUserInfo", requiredPermission = false)
    public ResponseData getUserInfo(){
         LoginUser loginUser= LoginContext.me().getLoginUser();
         //LoginResponse loginResponse = authServiceApi.loginWithUserName(loginUser.getAccount());
         LoginResponse loginResponse =new LoginResponse("");
         loginResponse.setLoginUser(loginUser);
         //给用户添加一些额外数据
         loginResponse.getLoginUser().setDataMap(userDataMapApi.setDateMap());
         return new SuccessResponseData(loginResponse);

    }


    /**
     * 基于token登录，适用于单点登录，将caToken请求过来，进行解析，并创建本系统可以识别的token
     *
     * @author holly
     * @date 2021/5/25 22:36
     */
    @ApiOperation(value = "适用于单点登录" ,notes = "适用于单点登录")
    @PostResource(name = "适用于单点登录", path = "/loginWithToken", requiredLogin = false, requiredPermission = false, responseClass = String.class)
    public ResponseData loginWithToken(@RequestBody @Validated LoginWithTokenRequest loginWithTokenRequest) {
        LoginResponse loginResponse = authServiceApi.LoginWithToken(loginWithTokenRequest);
        return new SuccessResponseData(loginResponse.getToken());
    }

    /**
     * 用户登出
     *
     * @author holly
     * @date 2021/3/17 17:24
     */
    @ApiOperation(value = "登出" ,notes = "登出")
    @GetResource(name = "登出", path = "/logoutAction", requiredPermission = false)
    public ResponseData logoutAction() {
        authServiceApi.logout();
        return new SuccessResponseData();
    }

    /**
     * 获取当前用户的用户信息
     *
     * @author holly
     * @date 2021/3/17 17:37
     */
    @ApiOperation(value = "获取当前用户的用户信息" ,notes = "获取当前用户的用户信息")
    @GetResource(name = "获取当前用户的用户信息", path = "/getCurrentLoginUserInfo", requiredPermission = false, responseClass = CurrentUserInfoResponse.class)
    public ResponseData getCurrentLoginUserInfo() {
        LoginUser loginUser = LoginContext.me().getLoginUser();

        // 转化返回结果
        CurrentUserInfoResponse currentUserInfoResponse = UserLoginInfoFactory.parseUserInfo(loginUser);

        return new SuccessResponseData(currentUserInfoResponse);
    }

    /**
     * 校验token是否正确
     *
     * @author holly
     * @date 2021/6/18 15:26
     */
    @ApiOperation(value = "校验token是否正确" ,notes = "校验token是否正确")
    @PostResource(name = "校验token是否正确", path = "/validateToken", requiredPermission = false, requiredLogin = false)
    public ResponseData validateToken(@RequestBody @Valid ValidateTokenRequest validateTokenRequest) {
        boolean haveSessionFlag = sessionManagerApi.haveSession(validateTokenRequest.getToken());
        return new SuccessResponseData(haveSessionFlag);
    }

}
